//
//  LoginViewController.m
//  MyBlog
//
//  Created by zhen7216 on 2017/5/13.
//  Copyright © 2017年 chenzhen. All rights reserved.
//

#import "LoginViewController.h"
#import "RegistViewController.h"
#import "LoginReturnJson.h"
#import "AMNetworkMngTool.h"

@interface LoginViewController ()<UITextFieldDelegate>
{
    NSString *_blockAccount;
    NSString *_blockPassword;
}
@property (weak, nonatomic) IBOutlet UIImageView *passwordTextBgImageView;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
@property (weak, nonatomic) IBOutlet UIImageView *accountTextBgImageView;
@property (weak, nonatomic) IBOutlet UITextField *accountTextField;
@property (weak, nonatomic) IBOutlet UIButton *loginButton;
@property (weak, nonatomic) IBOutlet UIImageView *tipImageView;
@property (weak, nonatomic) IBOutlet UILabel *tipLabel;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *loadingIndicatorView;
@property (weak, nonatomic) IBOutlet UIButton *registButton;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _accountTextField.delegate = self;
    _passwordTextField.delegate = self;
    
    UIImage *inputBgImage = [_accountTextBgImageView.image resizableImageWithCapInsets:UIEdgeInsetsMake(12, 22, 12, 22) resizingMode:UIImageResizingModeStretch];
    [_accountTextBgImageView setImage:inputBgImage];
    [_passwordTextBgImageView setImage:inputBgImage];
    
    UIImage *loginBgImage = [[UIImage imageNamed:@"button-green"] resizableImageWithCapInsets:UIEdgeInsetsMake(16, 16, 16, 16) resizingMode:UIImageResizingModeStretch];
    [_loginButton setBackgroundImage:loginBgImage forState:UIControlStateNormal];
    [_loginButton setBackgroundImage:loginBgImage forState:UIControlStateHighlighted];
    [_loginButton setBackgroundImage:loginBgImage forState:UIControlStateDisabled];
    [_loginButton setTitle:@"" forState:UIControlStateDisabled];
    
    [_tipLabel setHidden:YES];
    [_tipImageView setHidden:YES];
    [_loadingIndicatorView stopAnimating];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//- (void)loginButtonPressed {
//    NSLog(@"pressed");
//}

#pragma mark - button

- (IBAction)loginButtonAction:(id)sender {
    NSLog(@"登录");
    [_loginButton setEnabled:NO];
    [self.view endEditing:YES];
    [_loadingIndicatorView startAnimating];
    [_loadingIndicatorView setAlpha:0.5f];
    [UIView animateWithDuration:0.5f animations:^{
        [_loadingIndicatorView setAlpha:1.f];
    }completion:^(BOOL finished) {
        [_loadingIndicatorView stopAnimating];
        
        [self canLogin];
        //[self showBlogViewController];

        [_loginButton setEnabled:YES];
        }
    ];

}

- (IBAction)registButtonAction:(id)sender {
    [_tipLabel setHidden:YES];
    [_tipImageView setHidden:YES];
    RegistViewController *viewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"registViewController"];
    [viewController initDataBlock:^(NSString *account, NSString *password) {
        _blockAccount = account;
        _blockPassword = password;
        
    }];
    [self presentViewController:viewController animated:YES completion:nil];
}

#pragma mark - 此处有BUG-当用户名或密码错误时无法显示tipImageView

- (void)canLogin {
    __block NSString *errorTip = @"";
    if (_accountTextField.text.length == 0) {
        errorTip = @"请输入用户名";
        [_tipImageView setHidden:NO];
        [_tipLabel setHidden:NO];
        [_tipLabel setText:errorTip];
    } else if (_passwordTextField.text.length == 0) {
        errorTip = @"请输入密码";
        [_tipImageView setHidden:NO];
        [_tipLabel setHidden:NO];
        [_tipLabel setText:errorTip];
    } else {
        NSDictionary *parameters = @{@"username": _accountTextField.text,
                                     @"password": _passwordTextField.text};
        
        [[AMNetworkMngTool shareInstance] AMNetwork_LoginWithParameters:parameters withBlock:^(NSString *flag) {
            if ([flag isEqualToString:@"1"]) {
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    
                    [_tipLabel setHidden:YES];
                    [_tipImageView setHidden:YES];
                    
                    NSLog(@"登录成功");
                    
                   // [self showBlogViewController];
                    });
            } else {
                NSLog(@"登录失败");
                errorTip = @"用户名或密码错误";
            }
        }];
        if ([errorTip isEqualToString:@"用户名或密码错误"])
            [_tipImageView setHidden:NO];
            [_tipLabel setHidden:NO];
            [_tipLabel setText:@"用户名或密码错误"];
    }
}


//- (void)showBlogViewController {
////   TabBarViewController *viewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"testViewController"];
//    TabBarViewController *viewController = [[TabBarViewController alloc] init];
//    [self presentViewController:viewController animated:YES completion:nil];
//}


#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if (_accountTextField == textField) {
        [_passwordTextField becomeFirstResponder];
    } else if (_passwordTextField == textField) {
        [textField resignFirstResponder];
    }
    return NO;
}

//- (void)performSegueWithIdentifier:(NSString *)identifier sender:(id)sender {
//    
//}

//- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {
//    if ([self canLogin]) {
//        return YES;
//    } else {
//        [self loginButtonAction:nil];
//        return NO;
//    }
//}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
